﻿using System;
using OperationPlugins.Handlers;

namespace OperationPlugins.Models
{
    [HandlerOf(typeof(GetPropertyValueEnvelope), Inherited = false)]
    public abstract class GetPropertyValueHandler : ContextualPlugin, IHandler
    {
        /// <summary>
        /// Handles an object.
        /// </summary>
        /// <param name="value">The object to be handled.</param>
        /// <param name="context">The operation context.</param>
        void IHandler.Handle(object value, OperationContext context)
        {
            var envelope = (GetPropertyValueEnvelope)value;

            Value = envelope;

            Context = context;
           
            object propertyValue = Get(envelope.Model, envelope.DeclaringType, envelope.PropertyName);

            envelope.ReturnValues.Add(propertyValue);
        }

        /// <summary>
        /// Get property value.
        /// </summary>
        /// <param name="model">The model object.</param>
        /// <param name="declaringType">The type that declares the property.</param>
        /// <param name="propertyName">The property name.</param>
        /// <returns>The property value.</returns>
        protected abstract object Get(object model, Type declaringType, string propertyName);

    }
}
